//JavaScript Doccument
var app = Common.initApp(['ngAnimate', 'ui.bootstrap', 'angular.filter']);
Common.initDirective(app);
app.controller('relevanceBusinessListController', ['$scope', '$http', 'uiGridConstants', '$templateCache',
    function ($scope, $http, uiGridConstants, $templateCache) {
        $scope.relevanceBusiness = {};
        $scope.relevanceBusinessGrid = {
            dependence : {uiGridConstants : uiGridConstants, $templateCache : $templateCache},
            gridID : 'relevanceBusinessGrid',
            height : function () {
                $(function () {
                    var gridHeight = ($('.edit-body').height() - 43-28-7) + "px";
                    angular.element($(".relevanceBusinessGrid")).css('height', gridHeight);
                });
                $(window).resize(function () {
                    var gridHeight = ($('.edit-body').height() - 43-28-7) + "px";
                    angular.element($(".relevanceBusinessGrid")).css('height', gridHeight);
                })
            },
            gridOptions : {
                enableVerticalScrollbar : false, //默认grid滚动条不显示，如果将false改为true就可显示滚动条
                enableHorizontalScrollbar :false,
				enableSelectAll : false,         //是否全选选择框按钮
				enableRowHeaderSelection : false, //是否选择框按钮
                columnDefs : [
                    {
                        name: 'YWXH',
                        displayName: ' 选择',
                        enableSorting: false,
                        cellClass: 'grid-col-center',
                        width: '5%',
                        cellTemplate:
                            '<div class="ui-grid-selection-row-header-buttons selectOne ng-scope">' +
                            '<input type="checkbox" style="" class="td-check" value="{{row.entity.YWXH}}" YWLX="{{row.entity.YWLX}}" YWBT="{{row.entity.YWBT}}" YWLXMC="{{row.entity.YWLXMC}}"/>' +
                            '</div>'
                    },
                    {name : 'ROW_ID', displayName : '序号', width : '5%', cellClass : 'grid-col-center',enableSorting:false},
                    {
                        name: 'YWLX',
                        displayName: ' 业务类型',
                        enableSorting: false,
                        cellClass: 'grid-col-center',
                        width: '20%'
                    },

                    {
                        name: 'YWBT',
                        displayName: ' 业务标题',
                        enableSorting: false,
                        cellClass: 'grid-col-left',
                        width: '75%'
                    }
				]
			},
			dataConfig : {
                url: Common.webRoot() + '/business/relevance/business/relevancebusinesscontroller/queryDataByType',
				data : $scope.searchData,
				appendSuccess: function (response) {
                    $scope.queryIsChecked();
				}
			},
			gridEvent : {
				_rowClick : function (row, $event) {
					Common.grid.rowClickStyle($event);

				},
				_pageChanged : function () {
					Common.page.pageChanged($scope, $http, $scope.relevanceBusinessGrid);
				},
				_refresh : function () {
					//按需要修改的查询条件
					$scope.relevanceBusinessGrid.dataConfig.data.MC = $scope.filterValue;
					Common.page.send($scope, $http, $scope.relevanceBusinessGrid);
				},
				_changeItemsPerPage : function (item) {
					$scope.relevanceBusinessGrid.dataConfig.data.pageSize = item;
					Common.page.send($scope, $http, $scope.relevanceBusinessGrid);
				},
				_sortChange : function () {
					$scope.relevanceBusinessGrid.dataConfig.data.MC = $scope.filterValue;
					Common.page.send($scope, $http, $scope.relevanceBusinessGrid);
				}

			}
		};
        Common.grid.initConfig($scope, $http, $scope.relevanceBusinessGrid);

        //按下Enter键触发的函数
        Common.onKeyPress(function () {
            //添加你需要的逻辑
            $scope.queryRelevanceBusiness();
        });

        //查询按钮绑定的函数
        $scope.queryRelevanceBusiness = function () {
            Common.page.pageChanged($scope, $http, $scope.relevanceBusinessGrid);
        };

        $scope.queryIsChecked = function () {
            var isChecked = {
                url: Common.webRoot() + '/business/relevance/business/relevancebusinesscontroller/queryRelevanceBusinesss',
                method: 'POST',
                data: {"LYYWXH": $scope.businessId},
                success: function (response) {
                    var data = response.data.list;
                    $("input[type='checkbox']").each(function (k, v) {
                        if(data[k]){
                            if ($(this).val() === data[k].MBYWXH) {
                                $(this).attr("checked", "checked");
                            }
                        }

                    })
                },
                error: function (response) {
                    throw new Error('请求[' + queryConfig.url + ']出错,[' + response.data + ']');
                }
            }
            Common.send($scope, $http, isChecked)
        }

        $scope.addRelevanceBusiness = function () {
            var selectedModel = $("input[type='checkbox']:checked");

            if (selectedModel == null || selectedModel.length < 1) {
                Common.dialog({
                    type: 'alert',
                    content: '请选中要关联的数据'
                });
            } else {
               var businessMap = {};
               var businessList = [];
                selectedModel.each(function (k,v) {
                    var map = {};
                    map.YWXH = $(this).val();
                    map.YWLX = $(this).attr("YWLX");
                    map.YWBT = $(this).attr("YWBT");
                    map.YWLXMC = $(this).attr("YWLXMC");

                    businessList.push(map);
                })
                businessMap.businessList = businessList;
                businessMap.businessId = $scope.businessId;
                submitRelevance($scope, $http, businessMap);
            }
        }

		//清空查询条件
		$scope.clearData = function(){
			for(var key in $scope.searchData){
			    if (key == 'type') continue;
                $scope.searchData[key] = '';
            }
			$scope.queryRelevanceBusiness();
		};

        //显示隐藏切换
	$scope.showorhide = function(){
		$('#showorhide-up').toggle('500');
		$('#showorhide-down').toggle('500');
		$('.query-body').toggle('500');
	}
    }]).filter('mapInfo', function() {
    return function(input){
        return input == '' ? 'AAA' : 'BBB';
    }
});


submitRelevance = function ($scope,$http,businessMap) {
    var submitConfig = {
        url: Common.webRoot() + '/business/relevance/business/relevancebusinesscontroller/saveRelevanceBusiness',
        method: 'POST',
        data: JSON.stringify(businessMap),
        success: function (repsonse) {
            Common.dialog({
                type: 'msg',
                content: '保存成功'
            });

            parent.layer.close(parent.layer.getFrameIndex(window.name));
        },
        error: function (response) {
            throw new Error('请求[' + deleteConfig.url + ']出错,[' + response.data + ']');
        }
    }
    Common.send($scope, $http, submitConfig)
}